package com.briup.bookstore.service.impl;


import com.briup.bookstore.mapper.ShopcarMapper;
import com.briup.bookstore.po.Shopcar;
import com.briup.bookstore.po.ext.ShopCarExtend;
import com.briup.bookstore.service.ShopCarService;
import com.briup.bookstore.utils.BeanCopyUtils;
import com.briup.bookstore.vo.ShopCarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author qinyc
 * @description 针对表【es_shopcar】的数据库操作Service实现
 * @createDate 2023-07-18 21:33:38
 */
@Service
public class ShopcarServiceImpl implements ShopCarService {

    @Autowired
    private ShopcarMapper shopcarMapper;

    @Override
    public List<ShopCarVO> findLists(Integer userId) {
        List<ShopCarExtend> list = shopcarMapper.findByUserId(userId);
        List<ShopCarVO> shopCarVOS = BeanCopyUtils.copyBeanList(list, ShopCarVO.class);
        return shopCarVOS;
    }

    @Override
    public void add(int userId, Integer bookId) {
        Shopcar shopcar = shopcarMapper.findByUserIdAndBookId(userId, bookId);
        if (Objects.isNull(shopcar)) {
            shopcarMapper.save(userId, bookId);
        } else {
            shopcarMapper.updateNumById(shopcar.getNum() + 1, shopcar.getId());
        }
    }

    @Override
    public void updateNum(Integer id, Integer num) {
        shopcarMapper.updateNumById(num, id);
    }

    @Override
    public void deleteByIds(Integer... ids) {
        for (Integer id : ids) {
            shopcarMapper.deleteById(id);
        }

    }

}




